package com.osgi.desktop.ui;

import com.osgi.desktop.services.ToolBarServiceImpl;
import com.osgi.desktop.services.ToolBarService;
import com.onetwork.core.resource.Resource;
import com.onetwork.core.resource.ResourceType;
import com.onetwork.core.ui.ONetworkFrame;
import com.osgi.desktop.actions.InstalarPluginsAction;
import com.osgi.desktop.services.TabbedPaneService;
import com.osgi.desktop.services.TabbedPaneServiceImpl;
import com.osgi.desktop.ui.util.UtilIcons;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenu;
import org.knopflerfish.framework.Framework;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;

public class OSGIFrame extends ONetworkFrame {
    private static final long serialVersionUID = 1L;
    private JMenu cadastrosMenu;
    private JMenu pluginsMenu;
    
    private InstalarPluginsAction instalarPluginsAction;
    
    public OSGIFrame() {
        super("SoftAgregator - 1.0.0");
    }
    
    @Override
    protected void configure() {
        this.pluginsMenu  = new JMenu("Ferramentas");
        this.pluginsMenu.setIcon(UtilIcons.ferramentaIcon());
        this.instalarPluginsAction = new InstalarPluginsAction(mainTabbedPane);
        Framework framework = Resource.get(ResourceType.OSGIFramework);
        try {
            BundleContext bundleContext = framework.getSystemBundleContext();
            bundleContext.registerService(ToolBarService.class.getName(), new ToolBarServiceImpl(this.cadastrosToolBar), new Properties());
            bundleContext.registerService(TabbedPaneService.class.getName(), new TabbedPaneServiceImpl(mainTabbedPane), new Properties());
            framework.launch(0);
        } catch (BundleException ex) {
            Logger.getLogger(OSGIFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void configureMenuBar() {
        this.mainMenuBar.add(this.pluginsMenu);
        this.configureCadastrosMenu();
        this.configuraPluginsMenu();
    }
    
    private void configureCadastrosMenu() {}
    
    private void configuraPluginsMenu(){
        this.pluginsMenu.add(instalarPluginsAction);
    }

    @Override
    protected void configureToolBar() {}

    @Override
    protected void configureMainWindow() {
//        BasicWindow basicWindow = abrirGerenciamentoDeCompeticaoAction.basicWindow();
//        int index = this.mainTabbedPane.indexOfComponent(basicWindow);
//        if (index == -1) this.mainTabbedPane.addTab(basicWindow.getName(), basicWindow.icon(), basicWindow);
//        this.mainTabbedPane.setSelectedComponent(basicWindow);
    }
}